What is d?
The 'd' npm package is a utility for defining property descriptors for JavaScript objects. It is often used to create properties with specific characteristics such as being non-enumerable, writable, or configurable, and can be used in object property definition contexts where you want more control over how properties behave.
What are d's main functionalities?
Defining non-enumerable properties
This code sample demonstrates how to define a non-enumerable property 'foo' with the value 'bar'. The property will not show up in for-in loops or Object.keys() calls.
{"foo": d('bar')}
Defining writable and configurable properties
This code sample shows how to define a property 'foo' that is both writable and configurable with the value 'bar'. This means the property value can be changed and the property descriptor can be modified after the initial definition.
{"foo": d(true, 'bar')}
Defining a getter and setter
This code sample illustrates defining a property 'foo' with a getter and setter function. The getter function returns the value of '_foo', and the setter function assigns the new value to '_foo'.
{"foo": d.gs(function() { return this._foo; }, function(value) { this._foo = value; })}
Other packages similar to d
define-property
The 'define-property' npm package is similar to 'd' in that it is used to define a new property on an object with a given descriptor. It is a simpler and smaller utility that might be preferred for its minimalistic approach, but 'd' offers a more fluent and feature-rich API.
d
Property descriptor factory
Originally derived from d package.
Defining properties with descriptors is very verbose:
var Account = function () {};
Object.defineProperties(Account.prototype, {
deposit: {
value: function () { },
configurable: true,
enumerable: false,
writable: true
},
withdraw: {
value: function () { },
configurable: true,
enumerable: false,
writable: true
},
balance: { get: function () { }, configurable: true, enumerable: false }
});
D cuts that to:
var d = require("d");
var Account = function () {};
Object.defineProperties(Account.prototype, {
deposit: d(function () { }),
withdraw: d(function () { }),
balance: d.gs(function () { })
});
By default, created descriptor follow characteristics of native ES5 properties, and defines values as:
{ configurable: true, enumerable: false, writable: true }
You can overwrite it by preceding value argument with instruction:
d("c", value);
d("ce", value);
d("e", value);
d.gs("e", value);
Installation
$ npm install d
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
Other utilities
autoBind(obj, props) (d/auto-bind)
Define methods which will be automatically bound to its instances
var d = require('d');
var autoBind = require('d/auto-bind');
var Foo = function () { this._count = 0; };
Object.defineProperties(Foo.prototype, autoBind({
increment: d(function () { ++this._count; });
}));
var foo = new Foo();
domEl.addEventListener('click', foo.increment, false);
lazy(obj, props) (d/lazy)
Define lazy properties, which will be resolved on first access
var d = require("d");
var lazy = require("d/lazy");
var Foo = function () {};
Object.defineProperties(Foo.prototype, lazy({ items: d(function () { return []; }) }));
var foo = new Foo();
foo.items.push(1, 2);
Tests
$ npm test
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.